home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsdparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  24.9 KB  |  860 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsdparam.c,v 1.4 2000/09/19 19:00:27 lpd Exp $ */
  20. /* Default device parameters for Ghostscript library */
  21. #include "memory_.h"        /* for memcpy */
  22. #include "string_.h"        /* for strlen */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsdevice.h"        /* for prototypes */
  26. #include "gsparam.h"
  27. #include "gxdevice.h"
  28. #include "gxfixed.h"
  29.  
  30. /* Define whether we accept PageSize as a synonym for MediaSize. */
  31. /* This is for backward compatibility only. */
  32. #define PAGESIZE_IS_MEDIASIZE
  33.  
  34. /* ================ Getting parameters ================ */
  35.  
  36. /* Forward references */
  37. private bool param_HWColorMap(P2(gx_device *, byte *));
  38.  
  39. /* Get the device parameters. */
  40. int
  41. gs_get_device_or_hw_params(gx_device * orig_dev, gs_param_list * plist,
  42.                bool is_hardware)
  43. {
  44.     /*
  45.      * We must be prepared to copy the device if it is the read-only
  46.      * prototype.
  47.      */
  48.     gx_device *dev;
  49.     int code;
  50.  
  51.     if (orig_dev->memory)
  52.     dev = orig_dev;
  53.     else {
  54.     code = gs_copydevice(&dev, orig_dev, plist->memory);
  55.     if (code < 0)
  56.         return code;
  57.     }
  58.     gx_device_set_procs(dev);
  59.     fill_dev_proc(dev, get_params, gx_default_get_params);
  60.     fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
  61.     fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
  62.     code = (is_hardware ?
  63.         (*dev_proc(dev, get_hardware_params)) (dev, plist) :
  64.         (*dev_proc(dev, get_params)) (dev, plist));
  65.     if (dev != orig_dev)
  66.     gx_device_retain(dev, false);  /* frees the copy */
  67.     return code;
  68. }
  69.  
  70. /* Standard ProcessColorModel values. */
  71. static const char *const pcmsa[] =
  72. {
  73.     "", "DeviceGray", "", "DeviceRGB", "DeviceCMYK"
  74. };
  75.  
  76. /* Get standard parameters. */
  77. int
  78. gx_default_get_params(gx_device * dev, gs_param_list * plist)
  79. {
  80.     int code;
  81.  
  82.     /* Standard page device parameters: */
  83.  
  84.     int mns = 1;
  85.     bool seprs = false;
  86.     gs_param_string dns, pcms;
  87.     gs_param_float_array msa, ibba, hwra, ma;
  88.     gs_param_string_array scna;
  89.  
  90. #define set_param_array(a, d, s)\
  91.   (a.data = d, a.size = s, a.persistent = false);
  92.  
  93.     /* Non-standard parameters: */
  94.  
  95.     int colors = dev->color_info.num_components;
  96.     int depth = dev->color_info.depth;
  97.     int GrayValues = dev->color_info.max_gray + 1;
  98.     int HWSize[2];
  99.     gs_param_int_array hwsa;
  100.     gs_param_float_array hwma, mhwra;
  101.  
  102.     /* Fill in page device parameters. */
  103.  
  104.     param_string_from_string(dns, dev->dname);
  105.     {
  106.     const char *cms = pcmsa[colors];
  107.  
  108.     /* We might have an uninitialized device with */
  109.     /* color_info.num_components = 0.... */
  110.     if (*cms != 0)
  111.         param_string_from_string(pcms, cms);
  112.     else
  113.         pcms.data = 0;
  114.     }
  115.     set_param_array(hwra, dev->HWResolution, 2);
  116.     set_param_array(msa, dev->MediaSize, 2);
  117.     set_param_array(ibba, dev->ImagingBBox, 4);
  118.     set_param_array(ma, dev->Margins, 2);
  119.     set_param_array(scna, NULL, 0);
  120.  
  121.     /* Fill in non-standard parameters. */
  122.  
  123.     HWSize[0] = dev->width;
  124.     HWSize[1] = dev->height;
  125.     set_param_array(hwsa, HWSize, 2);
  126.     set_param_array(hwma, dev->HWMargins, 4);
  127.     set_param_array(mhwra, dev->MarginsHWResolution, 2);
  128.  
  129.     /* Transmit the values. */
  130.  
  131.     if (
  132.  
  133.     /* Standard parameters */
  134.  
  135.     (code = param_write_name(plist, "OutputDevice", &dns)) < 0 ||
  136. #ifdef PAGESIZE_IS_MEDIASIZE
  137.     (code = param_write_float_array(plist, "PageSize", &msa)) < 0 ||
  138. #endif
  139.     (code = (pcms.data == 0 ? 0 :
  140.          param_write_name(plist, "ProcessColorModel", &pcms))) < 0 ||
  141.     (code = param_write_float_array(plist, "HWResolution", &hwra)) < 0 ||
  142.     (code = (dev->ImagingBBox_set ?
  143.          param_write_float_array(plist, "ImagingBBox", &ibba) :
  144.          param_write_null(plist, "ImagingBBox"))) < 0 ||
  145.     (code = param_write_float_array(plist, "Margins", &ma)) < 0 ||
  146.     (code = param_write_int(plist, "MaxSeparations", &mns)) < 0 ||
  147.     (code = (dev->NumCopies_set < 0 ||
  148.          (*dev_proc(dev, get_page_device))(dev) == 0 ? 0:
  149.          dev->NumCopies_set ?
  150.          param_write_int(plist, "NumCopies", &dev->NumCopies) :
  151.          param_write_null(plist, "NumCopies"))) < 0 ||
  152.     (code = param_write_name_array(plist, "SeparationColorNames", &scna)) < 0 ||
  153.     (code = param_write_bool(plist, "Separations", &seprs)) < 0 ||
  154.     (code = param_write_bool(plist, "UseCIEColor", &dev->UseCIEColor)) < 0 ||
  155.  
  156.     /* Non-standard parameters */
  157.  
  158.     (code = param_write_int_array(plist, "HWSize", &hwsa)) < 0 ||
  159.     (code = param_write_float_array(plist, ".HWMargins", &hwma)) < 0 ||
  160.     (code = param_write_float_array(plist, ".MarginsHWResolution", &mhwra)) < 0 ||
  161.     (code = param_write_float_array(plist, ".MediaSize", &msa)) < 0 ||
  162.     (code = param_write_string(plist, "Name", &dns)) < 0 ||
  163.     (code = param_write_int(plist, "Colors", &colors)) < 0 ||
  164.     (code = param_write_int(plist, "BitsPerPixel", &depth)) < 0 ||
  165.     (code = param_write_int(plist, "GrayValues", &GrayValues)) < 0 ||
  166.     (code = param_write_long(plist, "PageCount", &dev->PageCount)) < 0 ||
  167.     (code = param_write_bool(plist, ".IgnoreNumCopies", &dev->IgnoreNumCopies)) < 0 ||
  168.     (code = param_write_int(plist, "TextAlphaBits",
  169.                 &dev->color_info.anti_alias.text_bits)) < 0 ||
  170.     (code = param_write_int(plist, "GraphicsAlphaBits",
  171.                 &dev->color_info.anti_alias.graphics_bits)) < 0
  172.  
  173.     )
  174.     return code;
  175.  
  176.     /* Fill in color information. */
  177.  
  178.     if (colors > 1) {
  179.     int RGBValues = dev->color_info.max_color + 1;
  180.     long ColorValues = 1L << depth;
  181.  
  182.     if ((code = param_write_int(plist, "RedValues", &RGBValues)) < 0 ||
  183.         (code = param_write_int(plist, "GreenValues", &RGBValues)) < 0 ||
  184.         (code = param_write_int(plist, "BlueValues", &RGBValues)) < 0 ||
  185.         (code = param_write_long(plist, "ColorValues", &ColorValues)) < 0
  186.         )
  187.         return code;
  188.     }
  189.     if (param_requested(plist, "HWColorMap")) {
  190.     byte palette[3 << 8];
  191.  
  192.     if (param_HWColorMap(dev, palette)) {
  193.         gs_param_string hwcms;
  194.  
  195.         hwcms.data = palette, hwcms.size = colors << depth,
  196.         hwcms.persistent = false;
  197.         if ((code = param_write_string(plist, "HWColorMap", &hwcms)) < 0)
  198.         return code;
  199.     }
  200.     }
  201.  
  202.     return 0;
  203. }
  204.  
  205. /* Get the color map for a device.  Return true if there is one. */
  206. private bool
  207. param_HWColorMap(gx_device * dev, byte * palette /* 3 << 8 */ )
  208. {
  209.     int depth = dev->color_info.depth;
  210.     int colors = dev->color_info.num_components;
  211.  
  212.     if (depth <= 8 && colors <= 3) {
  213.     byte *p = palette;
  214.     gx_color_value rgb[3];
  215.     gx_color_index i;
  216.  
  217.     fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
  218.     for (i = 0; (i >> depth) == 0; i++) {
  219.         int j;
  220.  
  221.         if ((*dev_proc(dev, map_color_rgb)) (dev, i, rgb) < 0)
  222.         return false;
  223.         for (j = 0; j < colors; j++)
  224.         *p++ = gx_color_value_to_byte(rgb[j]);
  225.     }
  226.     return true;
  227.     }
  228.     return false;
  229. }
  230.  
  231. /* Get hardware-detected parameters. Default action is no hardware params. */
  232. int
  233. gx_default_get_hardware_params(gx_device * dev, gs_param_list * plist)
  234. {
  235.     return 0;
  236. }
  237.  
  238. /* ---------------- Input and output media ---------------- */
  239.  
  240. /* Finish defining input or output media. */
  241. private int
  242. finish_media(gs_param_list * mlist, gs_param_name key, const char *media_type)
  243. {
  244.     int code = 0;
  245.  
  246.     if (media_type != 0) {
  247.     gs_param_string as;
  248.  
  249.     param_string_from_string(as, media_type);
  250.     code = param_write_string(mlist, key, &as);
  251.     }
  252.     return code;
  253. }
  254.  
  255. /* Define input media. */
  256.  
  257. const gdev_input_media_t gdev_input_media_default =
  258. {
  259.     gdev_input_media_default_values
  260. };
  261.  
  262. int 
  263. gdev_begin_input_media(gs_param_list * mlist, gs_param_dict * pdict,
  264.                int count)
  265. {
  266.     pdict->size = count;
  267.     return param_begin_write_dict(mlist, "InputAttributes", pdict, true);
  268. }
  269.  
  270. int
  271. gdev_write_input_media(int index, gs_param_dict * pdict,
  272.                const gdev_input_media_t * pim)
  273. {
  274.     char key[25];
  275.     gs_param_dict mdict;
  276.     int code;
  277.     gs_param_string as;
  278.  
  279.     sprintf(key, "%d", index);
  280.     mdict.size = 4;
  281.     code = param_begin_write_dict(pdict->list, key, &mdict, false);
  282.     if (code < 0)
  283.     return code;
  284.     if ((pim->PageSize[0] != 0 && pim->PageSize[1] != 0) ||
  285.     (pim->PageSize[2] != 0 && pim->PageSize[3] != 0)
  286.     ) {
  287.     gs_param_float_array psa;
  288.  
  289.     psa.data = pim->PageSize;
  290.     psa.size =
  291.         (pim->PageSize[0] == pim->PageSize[2] &&
  292.          pim->PageSize[1] == pim->PageSize[3] ? 2 : 4);
  293.     psa.persistent = false;
  294.     code = param_write_float_array(mdict.list, "PageSize",
  295.                        &psa);
  296.     if (code < 0)
  297.         return code;
  298.     }
  299.     if (pim->MediaColor != 0) {
  300.     param_string_from_string(as, pim->MediaColor);
  301.     code = param_write_string(mdict.list, "MediaColor",
  302.                   &as);
  303.     if (code < 0)
  304.         return code;
  305.     }
  306.     if (pim->MediaWeight != 0) {
  307.     /*
  308.      * We do the following silly thing in order to avoid
  309.      * having to work around the 'const' in the arg list.
  310.      */
  311.     float weight = pim->MediaWeight;
  312.  
  313.     code = param_write_float(mdict.list, "MediaWeight",
  314.                  &weight);
  315.     if (code < 0)
  316.         return code;
  317.     }
  318.     code = finish_media(mdict.list, "MediaType", pim->MediaType);
  319.     if (code < 0)
  320.     return code;
  321.     return param_end_write_dict(pdict->list, key, &mdict);
  322. }
  323.  
  324. int
  325. gdev_write_input_page_size(int index, gs_param_dict * pdict,
  326.                floatp width_points, floatp height_points)
  327. {
  328.     gdev_input_media_t media;
  329.  
  330.     media.PageSize[0] = media.PageSize[2] = width_points;
  331.     media.PageSize[1] = media.PageSize[3] = height_points;
  332.     media.MediaColor = 0;
  333.     media.MediaWeight = 0;
  334.     media.MediaType = 0;
  335.     return gdev_write_input_media(index, pdict, &media);
  336. }
  337.  
  338. int 
  339. gdev_end_input_media(gs_param_list * mlist, gs_param_dict * pdict)
  340. {
  341.     return param_end_write_dict(mlist, "InputAttributes", pdict);
  342. }
  343.  
  344. /* Define output media. */
  345.  
  346. const gdev_output_media_t gdev_output_media_default =
  347. {
  348.     gdev_output_media_default_values
  349. };
  350.  
  351. int 
  352. gdev_begin_output_media(gs_param_list * mlist, gs_param_dict * pdict,
  353.             int count)
  354. {
  355.     pdict->size = count;
  356.     return param_begin_write_dict(mlist, "OutputAttributes", pdict, true);
  357. }
  358.  
  359. int
  360. gdev_write_output_media(int index, gs_param_dict * pdict,
  361.             const gdev_output_media_t * pom)
  362. {
  363.     char key[25];
  364.     gs_param_dict mdict;
  365.     int code;
  366.  
  367.     sprintf(key, "%d", index);
  368.     mdict.size = 4;
  369.     code = param_begin_write_dict(pdict->list, key, &mdict, false);
  370.     if (code < 0)
  371.     return code;
  372.     code = finish_media(mdict.list, "OutputType", pom->OutputType);
  373.     if (code < 0)
  374.     return code;
  375.     return param_end_write_dict(pdict->list, key, &mdict);
  376. }
  377.  
  378. int 
  379. gdev_end_output_media(gs_param_list * mlist, gs_param_dict * pdict)
  380. {
  381.     return param_end_write_dict(mlist, "OutputAttributes", pdict);
  382. }
  383.  
  384. /* ================ Putting parameters ================ */
  385.  
  386. /* Forward references */
  387. private int param_anti_alias_bits(P3(gs_param_list *, gs_param_name, int *));
  388. private int param_MediaSize(P4(gs_param_list *, gs_param_name,
  389.                    const float *, gs_param_float_array *));
  390.  
  391. private int param_check_bool(P4(gs_param_list *, gs_param_name, bool, bool));
  392. private int param_check_long(P4(gs_param_list *, gs_param_name, long, bool));
  393.  
  394. #define param_check_int(plist, pname, ival, defined)\
  395.   param_check_long(plist, pname, (long)(ival), defined)
  396. private int param_check_bytes(P5(gs_param_list *, gs_param_name, const byte *, uint, bool));
  397.  
  398. #define param_check_string(plist, pname, str, defined)\
  399.   param_check_bytes(plist, pname, (const byte *)str, strlen(str), defined)
  400.  
  401. /* Set the device parameters. */
  402. /* If the device was open and the put_params procedure closed it, */
  403. /* return 1; otherwise, return 0 or an error code as usual. */
  404. int
  405. gs_putdeviceparams(gx_device * dev, gs_param_list * plist)
  406. {
  407.     bool was_open = dev->is_open;
  408.     int code;
  409.  
  410.     gx_device_set_procs(dev);
  411.     fill_dev_proc(dev, put_params, gx_default_put_params);
  412.     fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
  413.     code = (*dev_proc(dev, put_params)) (dev, plist);
  414.     return (code < 0 ? code : was_open && !dev->is_open ? 1 : code);
  415. }
  416.  
  417. /* Set standard parameters. */
  418. /* Note that setting the size or resolution closes the device. */
  419. /* Window devices that don't want this to happen must temporarily */
  420. /* set is_open to false before calling gx_default_put_params, */
  421. /* and then taking appropriate action afterwards. */
  422. int
  423. gx_default_put_params(gx_device * dev, gs_param_list * plist)
  424. {
  425.     int ecode = 0;
  426.     int code;
  427.     gs_param_name param_name;
  428.     gs_param_float_array hwra;
  429.     gs_param_int_array hwsa;
  430.     gs_param_float_array msa;
  431.     gs_param_float_array ma;
  432.     gs_param_float_array hwma;
  433.     gs_param_float_array mhwra;
  434.     gs_param_string_array scna;
  435.     int nci = dev->NumCopies;
  436.     int ncset = dev->NumCopies_set;
  437.     bool ignc = dev->IgnoreNumCopies;
  438.     bool ucc = dev->UseCIEColor;
  439.     gs_param_float_array ibba;
  440.     bool ibbnull = false;
  441.     int colors = dev->color_info.num_components;
  442.     int depth = dev->color_info.depth;
  443.     int GrayValues = dev->color_info.max_gray + 1;
  444.     int RGBValues = dev->color_info.max_color + 1;
  445.     long ColorValues = 1L << depth;
  446.     int tab = dev->color_info.anti_alias.text_bits;
  447.     int gab = dev->color_info.anti_alias.graphics_bits;
  448.     gs_param_string cms;
  449.  
  450.     /*
  451.      * Template:
  452.      *   BEGIN_ARRAY_PARAM(param_read_xxx_array, "pname", pxxa, size, pxxe) {
  453.      *     ... check value if desired ...
  454.      *     if (success)
  455.      *       break;
  456.      *     ... set ecode ...
  457.      *   } END_ARRAY_PARAM(pxxa, pxxe);
  458.      */
  459.  
  460. #define BEGIN_ARRAY_PARAM(pread, pname, pa, psize, e)\
  461.     BEGIN\
  462.     switch (code = pread(plist, (param_name = pname), &(pa))) {\
  463.       case 0:\
  464.     if ((pa).size != psize) {\
  465.       ecode = gs_note_error(gs_error_rangecheck);\
  466.       (pa).data = 0;    /* mark as not filled */\
  467.     } else
  468. #define END_ARRAY_PARAM(pa, e)\
  469.     goto e;\
  470.       default:\
  471.     ecode = code;\
  472. e:    param_signal_error(plist, param_name, ecode);\
  473.       case 1:\
  474.     (pa).data = 0;        /* mark as not filled */\
  475.     }\
  476.     END
  477.  
  478.     /*
  479.      * The HWResolution, HWSize, and MediaSize parameters interact in
  480.      * the following way:
  481.      *      1. Setting HWResolution recomputes HWSize from MediaSize.
  482.      *      2. Setting HWSize recomputes MediaSize from HWResolution.
  483.      *      3. Setting MediaSize recomputes HWSize from HWResolution.
  484.      * If more than one parameter is being set, we apply these rules
  485.      * in the order 1, 2, 3.  This does the right thing in the most
  486.      * common case of setting more than one parameter, namely,
  487.      * setting both HWResolution and HWSize.
  488.      */
  489.  
  490.     BEGIN_ARRAY_PARAM(param_read_float_array, "HWResolution", hwra, 2, hwre) {
  491.     if (hwra.data[0] <= 0 || hwra.data[1] <= 0)
  492.         ecode = gs_note_error(gs_error_rangecheck);
  493.     else
  494.         break;
  495.     } END_ARRAY_PARAM(hwra, hwre);
  496.     BEGIN_ARRAY_PARAM(param_read_int_array, "HWSize", hwsa, 2, hwsa) {
  497.     /* We need a special check to handle the nullpage device, */
  498.     /* whose size is legitimately [0 0]. */
  499.     if ((hwsa.data[0] <= 0 && hwsa.data[0] != dev->width) ||
  500.         (hwsa.data[1] <= 0 && hwsa.data[1] != dev->height)
  501.     )
  502.         ecode = gs_note_error(gs_error_rangecheck);
  503. #define max_coord (max_fixed / fixed_1)
  504. #if max_coord < max_int
  505.     else if (hwsa.data[0] > max_coord || hwsa.data[1] > max_coord)
  506.         ecode = gs_note_error(gs_error_limitcheck);
  507. #endif
  508. #undef max_coord
  509.     else
  510.         break;
  511.     } END_ARRAY_PARAM(hwsa, hwse);
  512.     {
  513.     const float *res = (hwra.data == 0 ? dev->HWResolution : hwra.data);
  514.  
  515. #ifdef PAGESIZE_IS_MEDIASIZE
  516.     const float *data;
  517.  
  518.     /* .MediaSize takes precedence over PageSize, so */
  519.     /* we read PageSize first. */
  520.     code = param_MediaSize(plist, "PageSize", res, &msa);
  521.     if (code < 0)
  522.         ecode = code;
  523.     /* Prevent data from being set to 0 if PageSize is specified */
  524.     /* but .MediaSize is not. */
  525.     data = msa.data;
  526.     code = param_MediaSize(plist, ".MediaSize", res, &msa);
  527.     if (code < 0)
  528.         ecode = code;
  529.     else if (msa.data == 0)
  530.         msa.data = data;
  531. #else
  532.     code = param_MediaSize(plist, ".MediaSize", res, &msa);
  533.     if (code < 0)
  534.         ecode = code;
  535. #endif
  536.     }
  537.  
  538.     BEGIN_ARRAY_PARAM(param_read_float_array, "Margins", ma, 2, me) {
  539.     break;
  540.     } END_ARRAY_PARAM(ma, me);
  541.     BEGIN_ARRAY_PARAM(param_read_float_array, ".HWMargins", hwma, 4, hwme) {
  542.     break;
  543.     } END_ARRAY_PARAM(hwma, hwme);
  544.     /* MarginsHWResolution cannot be changed, only checked. */
  545.     BEGIN_ARRAY_PARAM(param_read_float_array, ".MarginsHWResolution", mhwra, 2, mhwre) {
  546.     if (mhwra.data[0] != dev->MarginsHWResolution[0] ||
  547.         mhwra.data[1] != dev->MarginsHWResolution[1]
  548.     )
  549.         ecode = gs_note_error(gs_error_rangecheck);
  550.     else
  551.         break;
  552.     } END_ARRAY_PARAM(mhwra, mhwre);
  553.     switch (code = param_read_bool(plist, (param_name = ".IgnoreNumCopies"), &ignc)) {
  554.     default:
  555.         ecode = code;
  556.         param_signal_error(plist, param_name, ecode);
  557.     case 0:
  558.     case 1:
  559.         break;
  560.     }
  561.     if (dev->NumCopies_set >= 0 &&
  562.     (*dev_proc(dev, get_page_device))(dev) != 0
  563.     ) {
  564.     switch (code = param_read_int(plist, (param_name = "NumCopies"), &nci)) {
  565.     case 0:
  566.         if (nci < 0)
  567.         ecode = gs_error_rangecheck;
  568.         else {
  569.         ncset = 1;
  570.         break;
  571.         }
  572.         goto nce;
  573.     default:
  574.         if ((code = param_read_null(plist, param_name)) == 0) {
  575.         ncset = 0;
  576.         break;
  577.         }
  578.         ecode = code;    /* can't be 1 */
  579. nce:
  580.         param_signal_error(plist, param_name, ecode);
  581.     case 1:
  582.         break;
  583.     }
  584.     }
  585.     if ((code = param_read_bool(plist, (param_name = "UseCIEColor"), &ucc)) < 0) {
  586.     ecode = code;
  587.     param_signal_error(plist, param_name, ecode);
  588.     }
  589.     if ((code = param_anti_alias_bits(plist, "TextAlphaBits", &tab)) < 0)
  590.     ecode = code;
  591.     if ((code = param_anti_alias_bits(plist, "GraphicsAlphaBits", &gab)) < 0)
  592.     ecode = code;
  593.  
  594.     /* Ignore parameters that only have meaning for printers. */
  595. #define IGNORE_INT_PARAM(pname)\
  596.   { int igni;\
  597.     switch ( code = param_read_int(plist, (param_name = pname), &igni) )\
  598.       { default:\
  599.       ecode = code;\
  600.       param_signal_error(plist, param_name, ecode);\
  601.     case 0:\
  602.     case 1:\
  603.       break;\
  604.       }\
  605.   }
  606.     IGNORE_INT_PARAM("%MediaSource")
  607.     IGNORE_INT_PARAM("%MediaDestination")
  608.     switch (code = param_read_float_array(plist, (param_name = "ImagingBBox"), &ibba)) {
  609.     case 0:
  610.         if (ibba.size != 4 ||
  611.         ibba.data[2] < ibba.data[0] || ibba.data[3] < ibba.data[1]
  612.         )
  613.         ecode = gs_note_error(gs_error_rangecheck);
  614.         else
  615.         break;
  616.         goto ibbe;
  617.     default:
  618.         if ((code = param_read_null(plist, param_name)) == 0) {
  619.         ibbnull = true;
  620.         ibba.data = 0;
  621.         break;
  622.         }
  623.         ecode = code;    /* can't be 1 */
  624.       ibbe:param_signal_error(plist, param_name, ecode);
  625.     case 1:
  626.         ibba.data = 0;
  627.         break;
  628.     }
  629.  
  630.     /* Now check nominally read-only parameters. */
  631.     if ((code = param_check_string(plist, "OutputDevice", dev->dname, true)) < 0)
  632.     ecode = code;
  633.     if ((code = param_check_string(plist, "ProcessColorModel", pcmsa[colors], colors != 0)) < 0)
  634.     ecode = code;
  635.     if ((code = param_check_int(plist, "MaxSeparations", 1, true)) < 0)
  636.     ecode = code;
  637.     if ((code = param_check_bool(plist, "Separations", false, true)) < 0)
  638.     ecode = code;
  639.     BEGIN_ARRAY_PARAM(param_read_name_array, "SeparationColorNames", scna, 0, scne) {
  640.     break;
  641.     } END_ARRAY_PARAM(scna, scne);
  642.     if ((code = param_check_string(plist, "Name", dev->dname, true)) < 0)
  643.     ecode = code;
  644.     if ((code = param_check_int(plist, "Colors", colors, true)) < 0)
  645.     ecode = code;
  646.     if ((code = param_check_int(plist, "BitsPerPixel", depth, true)) < 0)
  647.     ecode = code;
  648.     if ((code = param_check_int(plist, "GrayValues", GrayValues, true)) < 0)
  649.     ecode = code;
  650.     if ((code = param_check_long(plist, "PageCount", dev->PageCount, true)) < 0)
  651.     ecode = code;
  652.     if ((code = param_check_int(plist, "RedValues", RGBValues, colors > 1)) < 0)
  653.     ecode = code;
  654.     if ((code = param_check_int(plist, "GreenValues", RGBValues, colors > 1)) < 0)
  655.     ecode = code;
  656.     if ((code = param_check_int(plist, "BlueValues", RGBValues, colors > 1)) < 0)
  657.     ecode = code;
  658.     if ((code = param_check_long(plist, "ColorValues", ColorValues, colors > 1)) < 0)
  659.     ecode = code;
  660.     if (param_read_string(plist, "HWColorMap", &cms) != 1) {
  661.     byte palette[3 << 8];
  662.  
  663.     if (param_HWColorMap(dev, palette))
  664.         code = param_check_bytes(plist, "HWColorMap", palette,
  665.                      colors << depth, true);
  666.     else
  667.         code = param_check_bytes(plist, "HWColorMap", 0, 0, false);
  668.     if (code < 0)
  669.         ecode = code;
  670.     }
  671.  
  672.     /* We must 'commit', in order to detect unknown parameters, */
  673.     /* even if there were errors. */
  674.     code = param_commit(plist);
  675.     if (ecode < 0)
  676.     return ecode;
  677.     if (code < 0)
  678.     return code;
  679.  
  680.     /* Now actually make the changes. */
  681.     /* Changing resolution or page size requires closing the device, */
  682.     /* but changing margins or ImagingBBox does not. */
  683.     /* In order not to close and reopen the device unnecessarily, */
  684.     /* we check for replacing the values with the same ones. */
  685.  
  686.     if (hwra.data != 0 &&
  687.     (dev->HWResolution[0] != hwra.data[0] ||
  688.      dev->HWResolution[1] != hwra.data[1])
  689.     ) {
  690.     if (dev->is_open)
  691.         gs_closedevice(dev);
  692.     gx_device_set_resolution(dev, hwra.data[0], hwra.data[1]);
  693.     }
  694.     if (hwsa.data != 0 &&
  695.     (dev->width != hwsa.data[0] ||
  696.      dev->height != hwsa.data[1])
  697.     ) {
  698.     if (dev->is_open)
  699.         gs_closedevice(dev);
  700.     gx_device_set_width_height(dev, hwsa.data[0], hwsa.data[1]);
  701.     }
  702.     if (msa.data != 0 &&
  703.     (dev->MediaSize[0] != msa.data[0] ||
  704.      dev->MediaSize[1] != msa.data[1])
  705.     ) {
  706.     if (dev->is_open)
  707.         gs_closedevice(dev);
  708.     gx_device_set_page_size(dev, msa.data[0], msa.data[1]);
  709.     }
  710.     if (ma.data != 0) {
  711.     dev->Margins[0] = ma.data[0];
  712.     dev->Margins[1] = ma.data[1];
  713.     }
  714.     if (hwma.data != 0) {
  715.     dev->HWMargins[0] = hwma.data[0];
  716.     dev->HWMargins[1] = hwma.data[1];
  717.     dev->HWMargins[2] = hwma.data[2];
  718.     dev->HWMargins[3] = hwma.data[3];
  719.     }
  720.     dev->NumCopies = nci;
  721.     dev->NumCopies_set = ncset;
  722.     dev->IgnoreNumCopies = ignc;
  723.     if (ibba.data != 0) {
  724.     dev->ImagingBBox[0] = ibba.data[0];
  725.     dev->ImagingBBox[1] = ibba.data[1];
  726.     dev->ImagingBBox[2] = ibba.data[2];
  727.     dev->ImagingBBox[3] = ibba.data[3];
  728.     dev->ImagingBBox_set = true;
  729.     } else if (ibbnull) {
  730.     dev->ImagingBBox_set = false;
  731.     }
  732.     dev->UseCIEColor = ucc;
  733.     dev->color_info.anti_alias.text_bits = tab;
  734.     dev->color_info.anti_alias.graphics_bits = gab;
  735.     gx_device_decache_colors(dev);
  736.     return 0;
  737. }
  738.  
  739. /* Read TextAlphaBits or GraphicsAlphaBits. */
  740. private int
  741. param_anti_alias_bits(gs_param_list * plist, gs_param_name param_name, int *pa)
  742. {
  743.     int code = param_read_int(plist, param_name, pa);
  744.  
  745.     switch (code) {
  746.     case 0:
  747.     switch (*pa) {
  748.     case 1: case 2: case 4:
  749.         return 0;
  750.     default:
  751.         code = gs_error_rangecheck;
  752.     }
  753.     default:
  754.     param_signal_error(plist, param_name, code);
  755.     case 1:
  756.     ;
  757.     }
  758.     return code;
  759. }
  760.  
  761.  
  762. /* Read .MediaSize or, if supported as a synonym, PageSize. */
  763. private int
  764. param_MediaSize(gs_param_list * plist, gs_param_name pname,
  765.         const float *res, gs_param_float_array * pa)
  766. {
  767.     gs_param_name param_name;
  768.     int ecode = 0;
  769.     int code;
  770.  
  771.     BEGIN_ARRAY_PARAM(param_read_float_array, pname, *pa, 2, mse) {
  772.     float width_new = pa->data[0] * res[0] / 72;
  773.     float height_new = pa->data[1] * res[1] / 72;
  774.  
  775.     if (width_new < 0 || height_new < 0)
  776.         ecode = gs_note_error(gs_error_rangecheck);
  777. #define max_coord (max_fixed / fixed_1)
  778. #if max_coord < max_int
  779.     else if (width_new > max_coord || height_new > max_coord)
  780.         ecode = gs_note_error(gs_error_limitcheck);
  781. #endif
  782. #undef max_coord
  783.     else
  784.         break;
  785.     } END_ARRAY_PARAM(*pa, mse);
  786.     return ecode;
  787. }
  788.  
  789. /* Check that a nominally read-only parameter is being set to */
  790. /* its existing value. */
  791. private int
  792. param_check_bool(gs_param_list * plist, gs_param_name pname, bool value,
  793.          bool defined)
  794. {
  795.     int code;
  796.     bool new_value;
  797.  
  798.     switch (code = param_read_bool(plist, pname, &new_value)) {
  799.     case 0:
  800.         if (defined && new_value == value)
  801.         break;
  802.         code = gs_note_error(gs_error_rangecheck);
  803.         goto e;
  804.     default:
  805.         if (param_read_null(plist, pname) == 0)
  806.         return 1;
  807.       e:param_signal_error(plist, pname, code);
  808.     case 1:
  809.         ;
  810.     }
  811.     return code;
  812. }
  813. private int
  814. param_check_long(gs_param_list * plist, gs_param_name pname, long value,
  815.          bool defined)
  816. {
  817.     int code;
  818.     long new_value;
  819.  
  820.     switch (code = param_read_long(plist, pname, &new_value)) {
  821.     case 0:
  822.         if (defined && new_value == value)
  823.         break;
  824.         code = gs_note_error(gs_error_rangecheck);
  825.         goto e;
  826.     default:
  827.         if (param_read_null(plist, pname) == 0)
  828.         return 1;
  829.       e:param_signal_error(plist, pname, code);
  830.     case 1:
  831.         ;
  832.     }
  833.     return code;
  834. }
  835. private int
  836. param_check_bytes(gs_param_list * plist, gs_param_name pname, const byte * str,
  837.           uint size, bool defined)
  838. {
  839.     int code;
  840.     gs_param_string new_value;
  841.  
  842.     switch (code = param_read_string(plist, pname, &new_value)) {
  843.     case 0:
  844.         if (defined && new_value.size == size &&
  845.         !memcmp((const char *)str, (const char *)new_value.data,
  846.             size)
  847.         )
  848.         break;
  849.         code = gs_note_error(gs_error_rangecheck);
  850.         goto e;
  851.     default:
  852.         if (param_read_null(plist, pname) == 0)
  853.         return 1;
  854.       e:param_signal_error(plist, pname, code);
  855.     case 1:
  856.         ;
  857.     }
  858.     return code;
  859. }
  860.